package com.firmboy.log.base.user.web;

import com.firmboy.log.base.bean.BaseMsg;
import com.firmboy.log.base.bean.PageParam;
import com.firmboy.log.base.user.model.User;
import com.firmboy.log.base.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * user的web层
 *
 * @author firmboy
 * @create 2018-03-17 下午3:03
 **/
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 增或改
     * @param user
     * @return
     */
    @RequestMapping(value = "/",method = RequestMethod.POST)
    public BaseMsg add(@RequestBody User user){
        BaseMsg msg = null;
        try {
            msg = userService.insertOrUpdateUser(user);
        }catch (Exception e){
            msg = BaseMsg.failMsg();
        }finally {
            return msg;
        }
    }

    /**
     * 删
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public BaseMsg delete(@PathVariable("id") String id){
        BaseMsg msg = null;
        try {
            msg = userService.deleteById(id);
        }catch (Exception e){
            msg = BaseMsg.failMsg();
        }finally {
            return msg;
        }
    }


    /**
     * 查单个根据id
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public BaseMsg get(@PathVariable("id") String id){
        BaseMsg msg = null;
        try {
            msg = userService.getUserById(id);
        }catch (Exception e){
            msg = BaseMsg.failMsg();
        }finally {
            return msg;
        }
    }

    /**
     * 查询所有
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public BaseMsg list(){
        BaseMsg msg = null;
        try {
            msg = userService.getUsers();
        }catch (Exception e){
            msg = BaseMsg.failMsg();
        }finally {
            return msg;
        }
    }


    @RequestMapping(value = "/page",method = RequestMethod.POST)
    public BaseMsg page(@PathVariable PageParam<User> param){
        BaseMsg msg = null;
        try {
            msg = userService.getUsers();
        }catch (Exception e){
            msg = BaseMsg.failMsg();
        }finally {
            return msg;
        }
    }
}
